package com.fuxi.cloud.desreport.model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * JmExpression
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */
public class JmExpression {
    public static final String REGEX = "^=${expression}\\((.*)\\.(.*)\\)$";
    public static final String[] EXP_TYPES = new String[]{"DBSUM", "DBMAX", "DBMIN", "DBAVERAGE"};
    public static final String REPLACE_STR = "${expression}";
    private String text;
    private String value;
    private String type;
    private String dbCode;
    private String field;

    public JmExpression() {
    }

    public JmExpression(String str) {
        this.text = str;
        this.value = "";

        for(int var2 = 0; var2 < EXP_TYPES.length; ++var2) {
            String var3 = EXP_TYPES[var2];
            if (str.indexOf(var3) > 0) {
                //这里的*原来是{
                String var4 = "^=$*expression}\\((.*)\\.(.*)\\)$".replace("${expression}", var3);
                Pattern var5 = Pattern.compile(var4);
                Matcher var6 = var5.matcher(str);
                if (var6.find()) {
                    this.type = var3;
                    this.dbCode = var6.group(1);
                    this.field = var6.group(2);
                    break;
                }
            }
        }

    }

    public String getFullSql(String metaSql, String condition) {
        String var3 = "";
        if (EXP_TYPES[0].equals(this.type)) {
            var3 = "SELECT sum(" + this.field + ") askey from ( " + metaSql + " )";
        } else if (EXP_TYPES[1].equals(this.type)) {
            var3 = "SELECT max(" + this.field + ") askey from ( " + metaSql + " )";
        } else if (EXP_TYPES[2].equals(this.type)) {
            var3 = "SELECT min(" + this.field + ") askey from ( " + metaSql + " )";
        } else if (EXP_TYPES[3].equals(this.type)) {
            var3 = "SELECT avg(" + this.field + ") askey from ( " + metaSql + " )";
        }

        var3 = var3 + "jeecg_rp_temp";
        if (condition != null && !"".equals(condition)) {
            var3 = var3 + " where 1=1 " + condition;
        }

        return var3;
    }

    public static Map<String, String> getExpMapByList(List<JmExpression> expList) {
        HashMap var1 = new HashMap();
        Iterator var2 = expList.iterator();

        while(var2.hasNext()) {
            JmExpression var3 = (JmExpression)var2.next();
            var1.put(var3.getText(), var3.getValue());
        }

        return var1;
    }

    public String getText() {
        return this.text;
    }

    public String getValue() {
        return this.value;
    }

    public String getType() {
        return this.type;
    }

    public String getDbCode() {
        return this.dbCode;
    }

    public String getField() {
        return this.field;
    }

    public void setText(String text) {
        this.text = text;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setDbCode(String dbCode) {
        this.dbCode = dbCode;
    }

    public void setField(String field) {
        this.field = field;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof JmExpression)) {
            return false;
        } else {
            JmExpression var2 = (JmExpression)o;
            if (!var2.canEqual(this)) {
                return false;
            } else {
                label71: {
                    String var3 = this.getText();
                    String var4 = var2.getText();
                    if (var3 == null) {
                        if (var4 == null) {
                            break label71;
                        }
                    } else if (var3.equals(var4)) {
                        break label71;
                    }

                    return false;
                }

                String var5 = this.getValue();
                String var6 = var2.getValue();
                if (var5 == null) {
                    if (var6 != null) {
                        return false;
                    }
                } else if (!var5.equals(var6)) {
                    return false;
                }

                label57: {
                    String var7 = this.getType();
                    String var8 = var2.getType();
                    if (var7 == null) {
                        if (var8 == null) {
                            break label57;
                        }
                    } else if (var7.equals(var8)) {
                        break label57;
                    }

                    return false;
                }

                String var9 = this.getDbCode();
                String var10 = var2.getDbCode();
                if (var9 == null) {
                    if (var10 != null) {
                        return false;
                    }
                } else if (!var9.equals(var10)) {
                    return false;
                }

                String var11 = this.getField();
                String var12 = var2.getField();
                if (var11 == null) {
                    if (var12 == null) {
                        return true;
                    }
                } else if (var11.equals(var12)) {
                    return true;
                }

                return false;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof JmExpression;
    }

    @Override
    public int hashCode() {
        boolean var1 = true;
        byte var2 = 1;
        String var3 = this.getText();
        int var8 = var2 * 59 + (var3 == null ? 43 : var3.hashCode());
        String var4 = this.getValue();
        var8 = var8 * 59 + (var4 == null ? 43 : var4.hashCode());
        String var5 = this.getType();
        var8 = var8 * 59 + (var5 == null ? 43 : var5.hashCode());
        String var6 = this.getDbCode();
        var8 = var8 * 59 + (var6 == null ? 43 : var6.hashCode());
        String var7 = this.getField();
        var8 = var8 * 59 + (var7 == null ? 43 : var7.hashCode());
        return var8;
    }

    @Override
    public String toString() {
        return "JmExpression(text=" + this.getText() + ", value=" + this.getValue() + ", type=" + this.getType() + ", dbCode=" + this.getDbCode() + ", field=" + this.getField() + ")";
    }
}

